<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class LibGoogleSitemap {
	var $items = array();
	
    var $header = "<\x3Fxml version=\"1.0\" encoding=\"UTF-8\"\x3F>\n\t<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
    var $charset = "UTF-8";
    var $footer = "\t</urlset>\n";
    var $items = array();
	
	
	//constructor
	function LibGoogleSitemap() {
		
	}
	
	//methods
	//Item	
	//	loc				location
	//	lastmod			YYYY-MM-DD
	//	changefreq		always, hourly, daily, weekly, monthly, yearly, never
	//	priority		0, 1
	function addItem($item) {
		$this->items[] = $item;
	}
	
	function add($loc, $lastmod = '', $changefreq = '', $priority = 0) {
		$item = array(
				'loc' => $loc,
				'lastmod' => $lastmod,
				'changefreq' => $changefreq,
				'priority' => $priority,
					);
		
		$this->items[] = $item;
	}
	
	function toString() {
		$xml = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
		$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
		foreach($this->items as $k => $item) {
			$xml .= "\t\t" . '<url>';
			$item['loc'] = htmlentities($item['loc'], ENT_QUOTES);
			
			$xml .= "\t\t\t" . '<loc>' . $item['loc'] . '</loc>' . "\n";
			if( !empty($item['lastmod']) ) {
				$xml .= "\t\t\t" . '<lastmod>' . $item['lastmod'] . '</lastmod>' . "\n";
			}
			if( !empty($item['changefreq']) ) {
				$xml .= "\t\t\t" . '<changefreq>' . $item['changefreq'] . '</changefreq>' . "\n";
			}
			if( !empty($item['priority']) ) {
				$xml .= "\t\t\t" . '<priority>' . $item['priority'] . '</priority>' . "\n";
			}
			$xml .= "\t\t" . '</url>' . "\n";
		}		
		$xml .= '</urlset>' . "\n";
		
		return $xml;
	}
}
//
?>